﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SunshineAirlines
{
    public partial class AddEdit_User : Form
    {
        public AddEdit_User()
        {
            InitializeComponent();
        }
        OpenFileDialog ofd = new OpenFileDialog();

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void AddEdit_User_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            string eil = textBox1.Text.Trim();
            string fnm = textBox2.Text.Trim();
            string lnm = textBox4.Text.Trim();
            string pho = textBox3.Text.Trim();
            //密码取前六位
            string pwd = eil.Split('@')[0];
            int len = pwd.Length;
            if (len > 6)
            {
                pwd = pwd.Substring(0, 6);
            }
            
            string add = textBox5.Text.Trim();
            string date = dateTimePicker1.Value.ToString("yyyy-MM-dd");
            //pictureBox1.Image = null;
            string role ="";
            string gen="";
            if (radioButton1.Checked)
            {
                role = "1";
            }
            if (radioButton2.Checked)
            {
                role = "2";
            }
            if (radioButton3.Checked)
            {
                gen = "F";
            }
            if (radioButton4.Checked)
            {
                gen = "M";
            }
            if (pictureBox1.Image != null)
            {
                pictureBox1.Image = System.Drawing.Image.FromFile(ofd.FileName);//未选图片会报错
                string sql = "if exists(select UserId from Users where Email='" + eil + "') update Users set FirstName = '" + fnm + "', LastName = '" + lnm + "', Phone = '" + pho + "', DateOfBirth = '" + date + "' where Email = '" + eil + "'else insert into Users(email, FirstName, LastName,Password, Gender, DateOfBirth, phone, Photo, Address,RoleId) values('" + eil + "', '" + fnm + "', '" + lnm + "','"+pwd+"' , '" + gen + "', '" + date + "', '" + pho + "', '" + pictureBox1.Image + "', '" + add + "','" + role + "')";
                DBHelper.ExecuteScalar(sql);
                MessageBox.Show("ok");
            }
            else
            {   //去掉picture
                string sql = "if exists(select UserId from Users where Email='" + eil + "') update Users set FirstName = '" + fnm + "', LastName = '" + lnm + "', Phone = '" + pho + "', DateOfBirth = '" + date + "' where Email = '" + eil + "'else insert into Users(email, FirstName, LastName,Password, Gender, DateOfBirth, phone, Photo, Address,RoleId) values('" + eil + "', '" + fnm + "', '" + lnm + "','"+pwd+"' , '" + gen + "', '" + date + "', '" + pho + "', '', '" + add + "','" + role + "')";
                DBHelper.ExecuteScalar(sql);
                MessageBox.Show("ok");

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {   //图片
            ofd.Filter = "Image Files|*.jpg";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                System.IO.FileInfo img = new System.IO.FileInfo(ofd.FileName);
                if (img.Length <= 102400)//计算大小B
                {
                    pictureBox1.ImageLocation = ofd.FileName;//填入picturebox
                }
                else
                {
                    MessageBox.Show("so big ");
                }

            }

        }
    }
}
